﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MaLu.Data;
using MaLu.Common;
using MaLu.BusiLogic;
using System.Text;
using MaLu.Framework.Common;

namespace MaluJg.web.StatsIn
{
    public partial class UCold : SUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
             

            }

        }
        public void SaveData()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("declare @id int; set @id={0}; select @id=id from FColdStorageDetails where id=@id;", RecordID);
            sb.AppendLine("if @@rowcount =0 begin");
            sb.AppendFormat(@"INSERT INTO [FColdStorageDetails]
           ([VillageID],[FamilyName],[IsPartner],[Days],[RentPerDay],[Rent],[Year])
     VALUES
           ({0},{1},'{2}',{3},{4},{5},{6}); end else begin

UPDATE [FColdStorageDetails]
   SET [VillageID] = {0},[FamilyName] = {1}
      ,[IsPartner] = {2}
      ,[Days] ={3}
      ,[RentPerDay] = {4}    
      ,[Rent] = {5}
      ,[Year] = {6}
 WHERE id=@id ; end", ddlVillage.SelectedValue, Util.ReturnSqlValue(tbName.Text), cbIsMember.Checked ? "1" : "0", Util.ReturnSqlValue(tbDays.Text.Trim()),
                   20, Util.ReturnSqlValue(tbMoney.Text), tbYear.Text);

            SqlHelper.Instance.ExecuteNonQuery(Provider, sb.ToString());

        }
        public void LoadData()
        {
            ddlVillage.DataSource = BLFarm.GetFType(Provider);
            ddlVillage.DataTextField = "name";
            ddlVillage.DataValueField = "id";
            ddlVillage.DataBind();
            DataTable dt = SqlHelper.Instance.ExecuteDataTable(Provider, @"SELECT *  FROM [FColdStorageDetails] where id=" + RecordID);
            if (dt.Rows.Count > 0)
            {
                LinkButton lbtnSave = Page.Master.FindControl("ToolBarContentPlaceHolder").FindControl("lbtnSave") as LinkButton;

                lbtnSave.Text = "<span ><img src=\"../Images/Icon/Action/save.gif\" alt=\"保存\" /></span><span>保存</span>";
                Page.Title = "扶持资金编辑";
                DataRow dr = dt.Rows[0];
                tbYear.Text = dr["Year"].ToString();
                tbName.Text = dr["FamilyName"].ToString();
                cbIsMember.Checked = dr["IsPartner"].ToString().ToLower() == "true" ? true : false;
                tbDays.Text = dr["days"].ToString();
                tbMoney.Text = dr["Rent"].ToString();
                ListItem li = ddlVillage.Items.FindByValue(dr["VillageID"].ToString());
                if (li != null)
                    ddlVillage.SelectedValue = li.Value;
            }
        }
    }
}